﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using DotNetWarcraft.WoWGuildRoster.Business;
using DotNetWarcraft.WoWGuildRoster.Business.Exceptions;

namespace UnitTests {
  /// <summary>
  /// Summary description for UnitTest1
  /// </summary>
  [TestClass]
  public class UnitTest1 {
    public UnitTest1() {
      //
      // TODO: Add constructor logic here
      //
    }

    private TestContext testContextInstance;
    private String testDbConnectionString = "Data Source=(local);Initial Catalog=DnnDvlp5;Integrated Security=True";

    private GuildEntry[] _entries;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext {
      get {
        return testContextInstance;
      }
      set {
        testContextInstance = value;
      }
    }

    [TestInitialize]
    public void SetData() {
      _entries = new GuildEntry[]
      {
        new GuildEntry(1) {Char_Class="druid", Char_Level=80, Char_Name="Dhrydan"},
        new GuildEntry(2) {Char_Class="hunter", Char_Level=80, Char_Name="Zodan"},
        new GuildEntry(3) {Char_Class="paladin", Char_Level=80, Char_Name="Kemel"},
        new GuildEntry(4) {Char_Class="priest", Char_Level=80, Char_Name="Thornkiss"},
        new GuildEntry(5) {Char_Class="warrior", Char_Level=70, Char_Name="Sabre"},
        new GuildEntry(6) {Char_Class="mage", Char_Level=61, Char_Name="Dhrymage"},
        new GuildEntry(7) {Char_Class="rogue", Char_Level=67, Char_Name="Chibi"},
        new GuildEntry(8) {Char_Class="warlock", Char_Level=73, Char_Name="Nieve"},
        new GuildEntry(9) {Char_Class="warrior", Char_Level=80, Char_Name="Jeidynzon"},
        new GuildEntry(10) {Char_Class="druid", Char_Level=70, Char_Name="Clearwood"}
      };
    }

    #region Additional test attributes
    //
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    // [ClassInitialize()]
    // public static void MyClassInitialize(TestContext testContext) { }
    //
    // Use ClassCleanup to run code after all tests in a class have run
    // [ClassCleanup()]
    // public static void MyClassCleanup() { }
    //
    // Use TestInitialize to run code before running each test 
    // [TestInitialize()]
    // public void MyTestInitialize() { }
    //
    // Use TestCleanup to run code after each test has run
    // [TestCleanup()]
    // public void MyTestCleanup() { }
    //
    #endregion

    [TestMethod]
    public void FilterEntity() {
      GuildEntry[] ge = _entries.Where(n => n.Char_Level < 80).ToArray<GuildEntry>();
      Assert.AreEqual(5, ge.Length);
    }

    [TestMethod]
    public void SortEntries() {
      GuildEntry[] ge = _entries.OrderByDescending(n => n.Char_Level).ToArray<GuildEntry>();

      int _lastLevel = int.MaxValue;

      foreach (GuildEntry _e in ge) {
        Assert.IsTrue(_e.Char_Level <= _lastLevel, _e.Char_Level.ToString() + " is not < " + _lastLevel.ToString()); 
        _lastLevel = _e.Char_Level;
      }
    }

    [TestMethod]
    public void DB_AllDatabaseEntries() {
      GuildRosterController.SetProvider("SQL");
      GuildRosterController.SetConnectionString(testDbConnectionString);

      GuildRoster[] _rosters = GuildRosterController.GetRosters();

      int _rosterId = -1;
      if (_rosters.Count() > 0) {
        _rosterId = _rosters[0].Roster_ID;
      }

      GuildEntry[] _entries;
      if (_rosterId > 0) {
        _entries = GuildRosterController.GetGuildEntries(_rosterId);
      } else {
        _entries = GuildRosterController.GetGuildEntries();
      }

      int _entryTest1 = -1, _entryTest2 = -1, _entryTest3 = -1, _entryTest4 = -1, _entryTest5 = -1;

      if (_entries.Count() >= 1) { _entryTest1 = _entries[0].Char_ID; }
      if (_entries.Count() >= 2) { _entryTest2 = _entries[1].Char_ID; }
      if (_entries.Count() >= 5) { _entryTest3 = _entries[4].Char_ID; }
      if (_entries.Count() >= 10) { _entryTest4 = _entries[9].Char_ID; }
      if (_entries.Count() >= 15) { _entryTest5 = _entries[14].Char_ID; }

      if (_entryTest1 > 0) { GuildEntry _e1 = GuildRosterController.GetGuildEntry(_entryTest1); }
      if (_entryTest2 > 0) { GuildEntry _e2 = GuildRosterController.GetGuildEntry(_entryTest2); }
      if (_entryTest3 > 0) { GuildEntry _e3 = GuildRosterController.GetGuildEntry(_entryTest3); }
      if (_entryTest4 > 0) { GuildEntry _e4 = GuildRosterController.GetGuildEntry(_entryTest4); }
      if (_entryTest5 > 0) { GuildEntry _e5 = GuildRosterController.GetGuildEntry(_entryTest5); }

      if (_entryTest1 > 0) {
        GuildEntryStat[] _stats = GuildRosterController.GetCharEntryStats(_entryTest1);
      }


    }

    [TestMethod]
    public void DB_DataLoads() {
      GuildRosterController.SetProvider("SQL");
      GuildRosterController.SetConnectionString(testDbConnectionString);

      DataLoad[] _loads = GuildRosterController.GetDataLoads();

      int _loadTest1 = -1, _loadTest2 = -1, _loadTest3 = -1, _loadTest4 = -1, _loadTest5 = -1;

      if (_loads.Count() >= 1) { _loadTest1 = _loads[0].Load_ID; }
      if (_loads.Count() >= 2) { _loadTest2 = _loads[1].Load_ID; }
      if (_loads.Count() >= 5) { _loadTest3 = _loads[4].Load_ID; }
      if (_loads.Count() >= 10) { _loadTest4 = _loads[9].Load_ID; }
      if (_loads.Count() >= 15) { _loadTest5 = _loads[14].Load_ID; }

      if (_loadTest1 > 0) { DataLoad _e1 = GuildRosterController.GetDataLoad(_loadTest1); }
      if (_loadTest2 > 0) { DataLoad _e2 = GuildRosterController.GetDataLoad(_loadTest2); }
      if (_loadTest3 > 0) { DataLoad _e3 = GuildRosterController.GetDataLoad(_loadTest3); }
      if (_loadTest4 > 0) { DataLoad _e4 = GuildRosterController.GetDataLoad(_loadTest4); }
      if (_loadTest5 > 0) { DataLoad _e5 = GuildRosterController.GetDataLoad(_loadTest5); }

    }

    [TestMethod]
    public void DB_Errors() {
      GuildRosterController.SetProvider("SQL");
      GuildRosterController.SetConnectionString(testDbConnectionString);

      WoWGuildRosterException[] _exceptions = GuildRosterController.GetErrors();

      int _errTest1 = -1, _errTest2 = -1, _errTest3 = -1, _errTest4 = -1, _errTest5 = -1;

      if (_exceptions.Count() >= 1) { _errTest1 = _exceptions[0].ErrorID; }
      if (_exceptions.Count() >= 2) { _errTest2 = _exceptions[1].ErrorID; }
      if (_exceptions.Count() >= 5) { _errTest3 = _exceptions[4].ErrorID; }
      if (_exceptions.Count() >= 10) { _errTest4 = _exceptions[9].ErrorID; }
      if (_exceptions.Count() >= 15) { _errTest5 = _exceptions[14].ErrorID; }

      if (_errTest1 > 0) { WoWGuildRosterException _e1 = GuildRosterController.GetError(_errTest1); }
      if (_errTest2 > 0) { WoWGuildRosterException _e2 = GuildRosterController.GetError(_errTest2); }
      if (_errTest3 > 0) { WoWGuildRosterException _e3 = GuildRosterController.GetError(_errTest3); }
      if (_errTest4 > 0) { WoWGuildRosterException _e4 = GuildRosterController.GetError(_errTest4); }
      if (_errTest5 > 0) { WoWGuildRosterException _e5 = GuildRosterController.GetError(_errTest5); }


      WoWGuildRosterException _exc = new GuildLoadError(new ApplicationException("No particular error"), 254, "Tested from Unit Testing");
      int _newErrId = GuildRosterController.AddError(_exc);

      WoWGuildRosterException _newErr = GuildRosterController.GetError(_newErrId); 
      GuildRosterController.DeleteError(_newErr);



      //
      // TODO: Add test logic	here
      //
    }

  
  }
}
